package com.sl.utils;

import java.lang.reflect.Field;
import java.util.Comparator;
import java.util.Date;

import org.apache.poi.ss.formula.functions.T;

public class ComparatorUtil implements Comparator {

	@Override
	public int compare(Object o1, Object o2) {
		try {
			Object oc1 = null;
			Object oc2 = null;
			Field [] f1 = o1.getClass().getSuperclass().getDeclaredFields();  
	        for(Field field:f1){
	        		if(field.getName().equals("createTime")) {
	        			field.setAccessible(true);
	        			oc1 = field.get(o1);
	        			break;
	            }
	        }

	        Field [] f2 = o2.getClass().getSuperclass().getDeclaredFields();  
	        for(Field field:f2){
		        	if(field.getName().equals("createTime")) {
		        		field.setAccessible(true);
		        		oc2 = field.get(o2);
		        		break;
		        	}
	        }
	        return ((Date) oc1).compareTo((Date) oc2);
	        
		}catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return 0;
	}

}
